package yx.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yx.dao.BaseDaoInt;
import yx.model.BsSms;
import yx.vo.BsSmsVO;
import yx.vo.DatagridVO;

@Service("bssmsService")
public class BsSmsServiceImpl implements BsSmsServiceInt {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(BsSmsServiceImpl.class);
	
	private BaseDaoInt<BsSms> smsDao;

	public BaseDaoInt<BsSms> getSmsDao() {
		return smsDao;
	}

	@Autowired
	public void setSmsDao(BaseDaoInt<BsSms> smsDao) {
		this.smsDao = smsDao;
	}

	@Override
	public BsSmsVO save(BsSmsVO sms) {
		BsSms t = new BsSms();
		t.setPhone(sms.getPhone());
		t.setContext(sms.getContext());
		t.setOptId(sms.getOptId());
		t.setStatus(sms.getStatus());
		t.setSendDate(new Date()); // 需要修改
		
		smsDao.save(t);
		BeanUtils.copyProperties(t, sms);
		return sms;
	}

	
	@Override
	public DatagridVO datagrid(BsSmsVO sms) {
		DatagridVO dg = new DatagridVO();
		String hql = "from BsSms t";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(sms, hql, params);
		String totalHql = "select count(*) "+hql;
		hql = addOrder(sms, hql);
		List<BsSms> l = smsDao.find(hql,params,sms.getPage(),sms.getRows());
		List<BsSmsVO> nl = new ArrayList<BsSmsVO>();
		changeModel(l, nl);
		dg.setTotal(smsDao.count(totalHql,params));
		dg.setRows(nl);
		return dg;
	}

	private void changeModel(List<BsSms> l, List<BsSmsVO> nl) {
		if(l != null && l.size() > 0){
			for(BsSms t : l){
				BsSmsVO u = new BsSmsVO();
				BeanUtils.copyProperties(t, u);
				nl.add(u);
			}
		}
	}

	private String addOrder(BsSmsVO user, String hql) {
		if(user.getSort() != null || user.getOrder() != null){
			hql += " order by "+user.getSort()+" " + user.getOrder();
		}
		return hql;
	}

	private String addWhere(BsSmsVO sms, String hql, Map<String, Object> params) {
		if(sms.getPhone() != null && !"".equals(sms.getPhone().trim())){
			hql += " where t.phone like :phone";
			params.put("phone", "%%"+sms.getPhone().trim()+"%%");
		}
		if(sms.getContext() != null && !"".equals(sms.getContext().trim())){
			hql += " where t.context like :context";
			params.put("context", "%%"+sms.getContext().trim()+"%%");
		}
		return hql;
	}

	@Override
	public void remove(String ids) {
//		for(String id : ids.split(",")){
//			SmUser u = userDao.get(SmUser.class, id);
//			if(u != null){
//				userDao.delete(u);				
//			}
//		}
		String[] nids = ids.split(",");
		String hql = "delete BsSms t where t.id in (";
		for(int i=0;i<nids.length;i++){
			if(i>0){
				hql += ",";
			}
			hql += "'" + nids[i] + "'";
		}
		hql += ")";
		smsDao.executeHql(hql);
	}

	@Override
	public BsSmsVO edit(BsSmsVO sms) {
		BsSms t = smsDao.get(BsSms.class, sms.getId());
		t.setContext(sms.getContext());
		t.setSendDate(new Date());
		t.setStatus(sms.getStatus());
		smsDao.update(t);
		return sms;
	}

	@Override
	public BsSms get(Integer id) {
		return smsDao.get(BsSms.class, id);
	}

	@Override
	public List<BsSms> queryBeSendInfo(String ids) {
		return smsDao.find("from BsSms b where b.id in ("+ids+") and b.status=1 ");
	}

}
